package com.ethan.minimalist.ui.login

import android.text.TextUtils
import android.text.method.PasswordTransformationMethod
import com.alibaba.android.arouter.facade.annotation.Route
import com.cxz.kotlin.baselibs.base.BaseMvpActivity
import com.cxz.kotlin.baselibs.config.ARouterPath
import com.cxz.kotlin.baselibs.ext.setSingleClickListener
import com.cxz.kotlin.baselibs.http.constant.HttpConstant
import com.ethan.minimalist.R
import com.ethan.minimalist.bean.UserBean
import com.ethan.minimalist.constant.Constant
import com.ethan.minimalist.mvp.contract.ILoginContract
import com.ethan.minimalist.mvp.presenter.LoginPresenter
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_main.top_view

/**
 * @ClassName:      LoginActivity
 * @Description:     登录页
 * @Author:         ethanWu
 * @CreateDate:     2020/3/17 10:51
 */
@Route(path = ARouterPath.Login)
class LoginActivity : BaseMvpActivity<ILoginContract.View, ILoginContract.Presenter>(),ILoginContract.View {

    private var isEyeOpen = false

    override fun createPresenter(): ILoginContract.Presenter {
        return LoginPresenter()
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_login
    }

    override fun initView() {
        super.initView()
        ImmersionBar.with(this@LoginActivity)
            .statusBarView(top_view)
            .statusBarDarkFont(true, 0.2f)
            .init()

        toolbar_title?.navigationIcon = resources.getDrawable(R.mipmap.icon_back)
        toolbar_title?.setNavigationOnClickListener { onBackPressed() }
    }

    override fun initData() {
        super.initData()
        tv_login.setSingleClickListener() {
            val username = et_account.text.toString()
            val password = et_password.text.toString()

            if (TextUtils.isEmpty(username)) {
                showDefaultMsg("账号不能为空")
                return@setSingleClickListener
            }

            if (TextUtils.isEmpty(password)) {
                showDefaultMsg("密码不能为空")
                return@setSingleClickListener
            }
            mPresenter?.login(username, password)
        }

        iv_close.setSingleClickListener {
            et_account.text = null
        }

        iv_eye.setOnClickListener {
            setPwdOpen()
        }
    }

    private fun setPwdOpen() {
        isEyeOpen = !isEyeOpen
        et_password.transformationMethod = if (isEyeOpen) null else PasswordTransformationMethod.getInstance()
        iv_eye.setImageResource(if (isEyeOpen) R.mipmap.icon_eye_open else R.mipmap.icon_eye)
        val str: String = et_password.text.toString()
        et_password.setSelection(if (TextUtils.isEmpty(str)) 0 else str.length)
    }

    override fun start() {

    }

    override fun loginSuccess(data: UserBean) {
        showMsg("登录成功")
        MMKV.defaultMMKV().encode(Constant.USER,Gson().toJson(data))
        MMKV.defaultMMKV().encode(HttpConstant.TOKEN_KEY,data?.token)
        finish()
    }

}